package cn.anquing.util;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ThumbUtil {
	
	
	    public static String  thumbnailsToSquareCenter(String imgFromPath,String imgName,String imgTopath) throws FileNotFoundException, IOException{
			
			//获取原图片
			File picture = new File(imgFromPath,imgName);		
	        BufferedImage sourceImg =ImageIO.read(new FileInputStream(picture)); 
	        //原图片高宽        
	        int width = sourceImg.getWidth();
	        int height = sourceImg.getHeight();
	        //缩略图片名
	        String thumbName =  "thumb_"+imgName;
	        
	        //正方形边长
	        int squareWidth = height;
	        if(width<height){
	        	squareWidth=width;
	        }
	        
	        Thumbnails.of(imgFromPath+imgName)
	        .sourceRegion(Positions.CENTER, squareWidth,squareWidth)
	        .size(300, 300)  
	        .keepAspectRatio(false) 
	        .toFile(imgTopath+thumbName);
	        
			return  thumbName;
		}

}
